package com.test.httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

/**
 * Created by yang on 17/3/7.
 */
public class StuSubmit {

    public static String list = null;
    public static List<String> questionId = new ArrayList<>();
    public static List<String> rightAnswer = new ArrayList<>();
    public static String homeworkId = null;

    public static void openHomework() throws Exception {

//        StuLogin.getToken();
        System.out.println("请输入homeworkID： ");
        // 输入homeworkId
        Scanner input = new Scanner(System.in);
        homeworkId = input.nextLine();
        // question-list请求url
//        String url = "http://stuapi.knowbox.cn/homework/once/question-list";
        // 构造question-list请求参数
        String url = "http://qa.knowbox.cn:8002/homework/once/question-list";
        for (int i = 0; i < StuLogin.token.size(); i++) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("token", StuLogin.token.get(i));
            params.put("homeworkID", homeworkId);
            System.out.println(params);
            list = Get.sendGet(url, params);

//            list = url+"?"+"token="+"hMXlaRwR8c6OwxRjBXhxxt6x3U5flA1Wnd6+0nH+/wdf+gWH1erL9IypNye1q5oS"+"&&homeworkID="+homeworkId;
         }
        System.out.println(list);

    }

    public static void getAnswer() throws Exception {
        StuSubmit.openHomework();
        if (StuSubmit.list != null && !StuSubmit.list.isEmpty()){
            JSONObject jsonList = new JSONObject(StuSubmit.list);
            JSONArray questionList = jsonList.optJSONArray("questionList");
            for (int i = 0; i < questionList.length(); i++) {
                JSONObject quesJson = questionList.getJSONObject(i);
                StuSubmit.questionId.add(quesJson.optString("questionID"));
                StuSubmit.rightAnswer.add(quesJson.optString("rightAnswer"));
            }
            System.out.println("----getAnswer is ok----");
        }else {
            System.out.println("----getAnser is fail");
        }

    }

    public static void main(String[] args) throws Exception {

        StuSubmit.getAnswer();

        String url = "http://stuapi.knowbox.cn/homework/once/commit";
//        String url = "http://qa.knowbox.cn:8002/homework/once/commit";
        JSONArray requesList = new JSONArray();
        // 获取答案list
        for (int i = 0; i < StuSubmit.questionId.size(); i++) {
            JSONObject requesOne = new JSONObject();
            requesOne.put("questionID", StuSubmit.questionId.get(i));
            requesOne.put("answer", StuSubmit.rightAnswer.get(i));
            requesOne.put("spendTime", "100000");
            requesList.put(requesOne);
        }

        for (int i = 0; i < StuLogin.token.size(); i++) {
//            Map<String, String> request = new HashMap<String, String>();
            JSONObject request = new JSONObject();
            request.put("list", requesList);
            request.put("token", "hMXlaRwR8c6OwxRjBXhxxt6x3U5flA1Wnd6+0nH+/wdf+gWH1erL9IypNye1q5oS");
            request.put("homeworkID", StuSubmit.homeworkId);
//            request.put("homeworkID", "'784500089'");
            System.out.println(request);
//            System.out.println(Post.sendPost(url, request));


            CloseableHttpClient httpClient = HttpClients.createDefault();

            HttpHost proxy = new HttpHost("192.168.51.203", 8888);
            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(config);
            httpPost.setEntity(new StringEntity(request.toString(),"utf-8"));
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.addHeader("User-Agent","RapidCalculation/3.0.0 (iPhone; iOS 8.1.2; Scale/2.00)");

            CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity,"utf-8"));
            response.close();
            httpClient.close();

        }
    }
}
